﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace projetoFinal
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                negative.Visible = false;

                if (User.Identity.IsAuthenticated == true)
                {
                    Server.Transfer("Default.aspx", false);
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            usuario u = operaUsuario.validaLogin(txtUsuario.Value.ToString(), txtSenha.Value.ToString());

            if (u != null)
            {
                negative.Visible = false;

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, u.CodUsuario.ToString(), DateTime.Now, DateTime.Now.AddHours(1), false, u.Nome.ToString());
                string encTicket = FormsAuthentication.Encrypt(ticket);
                //Cria o cookie
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);

                Response.Cookies.Add(authCookie);
                Response.Redirect(FormsAuthentication.GetRedirectUrl(u.CodUsuario.ToString(), false));
            }
            else
            {
                negative.Visible = true;
                lblTitleNegative.Text = "Erro!";
                lblMessageNegative.Text = "Usuário e/ou senha incorretos";
            }
        }
    }
}
